package com.example.erp.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.erp.config.error.CustomException;
import com.example.erp.entity.Goods;
import com.example.erp.entity.InoutBoundDtl;
import com.example.erp.entity.StoreInfo;
import com.example.erp.entity.dto.ExcelDto;
import com.example.erp.entity.dto.InoutBoundDto;
import com.example.erp.mapper.GoodsMapper;
import com.example.erp.mapper.InoutBoundDtlMapper;
import com.example.erp.mapper.StoreInfoMapper;
import com.example.erp.service.IGoodsService;
import com.example.erp.service.IInoutBoundDtlService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.erp.service.IStoreInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 出入库子单 服务实现类
 * </p>
 *
 * @author rws
 * @since 2025-01-08
 */
@Service
public class InoutBoundDtlServiceImpl extends ServiceImpl<InoutBoundDtlMapper, InoutBoundDtl> implements IInoutBoundDtlService {

    @Autowired
    private InoutBoundDtlMapper inoutBoundDtlMapper;

    @Autowired
    private IStoreInfoService iStoreInfoService;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private StoreInfoMapper storeInfoMapper;

    LocalDateTime now = LocalDateTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String time = now.format(formatter);

    //入库表
    @Override
    public void insertIn_bound_dtl(List<ExcelDto> excelDtoList, int id) {
        for(ExcelDto excelDto:excelDtoList){

            QueryWrapper<Goods> GoodsWrapper = new QueryWrapper<>();
            GoodsWrapper.allEq(Map.of("goods_name",excelDto.getGoodsName(),"units",excelDto.getUnits()));
            Goods goods=goodsMapper.selectOne(GoodsWrapper);

            InoutBoundDtl inoutBoundDtl=new InoutBoundDtl();
            inoutBoundDtl.setBoundId(id)
                    .setType(1)
                    .setGoodsCode(goods.getGoodsCode())
                    .setNum(excelDto.getNum())
                    .setWeight(excelDto.getWeight())
                    .setInoutTime(time)
                    .setCreateTime(time)
                    .setUpdateTime(time);

            inoutBoundDtlMapper.insert(inoutBoundDtl);


            excelDto.setBoundId(id);
            excelDto.setDtlId(inoutBoundDtl.getDtlId());
        }

        //更新库存表
        iStoreInfoService.insertStoreInfo(excelDtoList);
    }

    //待出库表
    @Override
    public void insertout_bound_dtl(List<ExcelDto> excelDtoList, int id) {
        for(ExcelDto excelDto:excelDtoList){

            QueryWrapper<Goods> GoodsWrapper = new QueryWrapper<>();
            GoodsWrapper.allEq(Map.of("goods_name",excelDto.getGoodsName(),"units",excelDto.getUnits()));
            Goods goods=goodsMapper.selectOne(GoodsWrapper);

            InoutBoundDtl inoutBoundDtl=new InoutBoundDtl();
            inoutBoundDtl.setBoundId(id)
                    .setType(2)
                    .setGoodsCode(goods.getGoodsCode())
                    .setNum(excelDto.getNum())
                    .setWeight(excelDto.getWeight())
                    .setCreateTime(time)
                    .setUpdateTime(time);


            inoutBoundDtlMapper.insert(inoutBoundDtl);


            excelDto.setBoundId(id);
            excelDto.setDtlId(inoutBoundDtl.getDtlId());
        }

        //更新库存表
        iStoreInfoService.insertStoreInfo(excelDtoList);
    }

    //出库
    @Override
    public void outStore(InoutBoundDto inoutBoundDto) {
        InoutBoundDtl inoutBoundDtl=new InoutBoundDtl();
        inoutBoundDtl.setDtlId(inoutBoundDto.getDtlId())
                .setInoutTime(time)
                .setUpdateTime(time);
        inoutBoundDtlMapper.updateById(inoutBoundDtl);

        StoreInfo storeInfo=new StoreInfo();
        storeInfo.setInfoId(inoutBoundDto.getInfoId())
                .setIsDelete(0)
                .setUpdateTime(time);
        storeInfoMapper.updateById(storeInfo);
    }
}
